<?php
  $objXajax->registerFunction("frmroom_save");
  $objXajax->registerFunction("frmroom_edit");
  $objXajax->registerFunction("frmroom_delete");
  $objXajax->registerFunction("frmroom_loadGrid");
  $objXajax->registerFunction("frmroom_fillRoomType");
  $hotel_id = $_SESSION['current']['hotel_id'] ;
  function frmroom_save($params){
    global $objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();
    $room_type_id = $params['cbbroomtype'];
    $strname = $params['name'];
    $strValue = frmroom_createValue($strname,$room_type_id);
    $sqlInsert = "insert into tbl_room(name,room_type) values$strValue";
    $flag = $objDbUpdate->Execute($sqlInsert);
    $objResponse->addScript('xajax_frmroom_loadGrid();');
    return $objResponse->getXML(); 
  }
  
  function frmroom_createValue($strname,$room_type_id){
      $strname = str_replace(',',';',$strname);
      $arrname = explode(';',$strname);
      $result ='';
      for($i=0;$i<count($arrname);$i++){
          $name =trim($arrname[$i]) ;
          if($name!=''){
              $result .= ",('$name',$room_type_id)"; 
          }         
      }
      if(strlen($result)>0){
        $result = substr($result,1);  
      }
      return $result;
  }
  
   function frmroom_loadGrid(){
    global $lang,$path;
    $objResponse = new xajaxResponse();
    $lang ='vi';
    $arrGrid = frmroom_GetGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_room_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmroom_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmroom_tbl');"); 
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
  }
  
    function  frmroom_GetGrid(){
        global $objDbSelect;
        $hotel_id = $_SESSION['current']['hotel_id'];
        $sqlSelect = "select r.id as id,r.active, r.name as name,rt.name roomtype from tbl_room r join tbl_room_type rt on r.room_type = rt.id and 
        rt.hotel_id = '$hotel_id'  order by rt.name,r.name";
        //echo $sqlSelect; die();
        return $objDbSelect->GetArray($sqlSelect);
    }
    function frmroom_delete($strId){
        global $objDbSelect;
       $objResponse = new xajaxResponse();
       $sql = " delete from tbl_room where id in ($strId) " ;
       $objDbSelect->Execute($sql);
       $objResponse->addScript('changeWaiting(1);');
       $objResponse->addScript('deleteRowlogic();');
       return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
    
     function frmroom_edit($id){
       global $objDbSelect;
       $objResponse = new xajaxResponse();
       $sql = " SELECT * FROM tbl_room WHERE id = '$id' ";
       $arr = $objDbSelect->GetArray($sql);
       
      $roomname        = $arr[0]['name'];
      $roomtype   = $arr[0]['room_type'];
      $objResponse->addScript("$('#frmroom_cbbroomtype').val($roomtype);");
      $objResponse->addScript("$('#frmroom_name').val('$roomname');");
      $objResponse->addScript('changeWaiting(1);');
      return $objResponse->getXML();   
    }
    function frmroom_fillRoomType(){
        global $objDbSelect;
        $objResponse = new xajaxResponse();
        $hotel_id = $_SESSION['current']['hotel_id'];
        $sql = "SELECT  * FROM tbl_room_type WHERE hotel_id = $hotel_id";
        $arr = $objDbSelect->GetArray($sql);
        $count = count($arr);
        for($i=0;$i<$count;$i++){
        $id = $arr[$i]['id'];
        $name = $arr[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
        }
        $temp =  '<select id="frmroom_cbbroomtype" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
        $objResponse->addAssign('frmroom_tdroomtype', 'innerHTML',$temp);
        return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
?>
